home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectMusic / AudioScripts / audioscripts.cpp next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  12.6 KB  |  353 lines

  1. //-----------------------------------------------------------------------------
  2. // File: AudioScripts.cpp
  3. //
  4. // Desc: Plays a script file using DirectMusic
  5. //
  6. // Copyright (c) 2000-2001 Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #define STRICT
  9. #include <windows.h>
  10. #include <basetsd.h>
  11. #include <commdlg.h>
  12. #include <objbase.h>
  13. #include <stdio.h>
  14. #include <dmusicc.h>
  15. #include <dmusici.h>
  16. #include <dxerr8.h>
  17. #include "resource.h"
  18. #include <tchar.h>
  19. #include "DMUtil.h"
  20. #include "DXUtil.h"
  21.  
  22.  
  23.  
  24.  
  25. //-----------------------------------------------------------------------------
  26. // Function-prototypes
  27. //-----------------------------------------------------------------------------
  28. INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam );
  29. HRESULT OnInitDialog( HWND hDlg );
  30. HRESULT OnChangeScriptFile( HWND hDlg, TCHAR* strFileName );
  31. HRESULT OnCallRoutine( HWND hDlg, TCHAR* strRoutine );
  32. HRESULT UpdateVariables( HWND hDlg );
  33. HRESULT SetVariable( HWND hDlg, int nIDDlgItem );
  34.  
  35.  
  36.  
  37.  
  38. //-----------------------------------------------------------------------------
  39. // Defines, constants, and global variables
  40. //-----------------------------------------------------------------------------
  41. CMusicManager* g_pMusicManager = NULL;
  42. CMusicScript*  g_pMusicScript  = NULL;
  43. HINSTANCE      g_hInst         = NULL;
  44.  
  45.  
  46.  
  47.  
  48. //-----------------------------------------------------------------------------
  49. // Name: WinMain()
  50. // Desc: Entry point for the application.  Since we use a simple dialog for 
  51. //       user interaction we don't need to pump messages.
  52. //-----------------------------------------------------------------------------
  53. INT APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, 
  54.                       INT nCmdShow )
  55. {
  56.     g_hInst = hInst;
  57.     
  58.     // Display the main dialog box.
  59.     DialogBox( hInst, MAKEINTRESOURCE(IDD_MAIN), NULL, MainDlgProc );
  60.     
  61.     return TRUE;
  62. }
  63.  
  64.  
  65.  
  66.  
  67. //-----------------------------------------------------------------------------
  68. // Name: MainDlgProc()
  69. // Desc: Handles dialog messages
  70. //-----------------------------------------------------------------------------
  71. INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
  72. {
  73.     HRESULT hr;
  74.  
  75.     switch( msg ) 
  76.     {
  77.         case WM_INITDIALOG:
  78.             if( FAILED( hr = OnInitDialog( hDlg ) ) )
  79.             {
  80.                 DXTRACE_ERR( TEXT("OnInitDialog"), hr );
  81.                 MessageBox( hDlg, "Error initializing DirectMusic.  Sample will now exit.", 
  82.                                   "DirectMusic Sample", MB_OK | MB_ICONERROR );
  83.                 EndDialog( hDlg, 0 );
  84.                 return TRUE;
  85.             }
  86.             break;
  87.  
  88.         case WM_COMMAND:
  89.             switch( LOWORD(wParam) )
  90.             {
  91.                 case IDC_COMBO_SCRIPT:
  92.                     if ( HIWORD(wParam) == CBN_SELCHANGE )
  93.                     {
  94.                         char strFile[MAX_PATH] = "";
  95.                         GetDlgItemText( hDlg, IDC_COMBO_SCRIPT, strFile, MAX_PATH );
  96.                         if( FAILED( hr = OnChangeScriptFile( hDlg, strFile ) ) )
  97.                         {
  98.                             DXTRACE_ERR( TEXT("OnChangeScriptFile"), hr );
  99.                             MessageBox( hDlg, "Error loading script.  Sample will now exit.", 
  100.                                               "DirectMusic Sample", MB_OK | MB_ICONERROR );
  101.                             EndDialog( hDlg, 0 );
  102.                             return TRUE;
  103.                         }
  104.                         break;
  105.                     }
  106.  
  107.                 case IDC_BUTTON_ROUTINE1:
  108.                 case IDC_BUTTON_ROUTINE2:
  109.                 case IDC_BUTTON_ROUTINE3:
  110.                     if ( HIWORD(wParam) == BN_CLICKED )
  111.                     {
  112.                         TCHAR pstrRoutine[] = TEXT("RoutineX");
  113.                         pstrRoutine[_tcslen(pstrRoutine) - 1] = '1' + (LOWORD(wParam) - IDC_BUTTON_ROUTINE1);
  114.                         if( FAILED( hr = OnCallRoutine(hDlg, pstrRoutine) ) ) 
  115.                         {
  116.                             DXTRACE_ERR( TEXT("OnCallRoutine"), hr );
  117.                             MessageBox( hDlg, "Error calling routine.  Sample will now exit.", 
  118.                                               "DirectMusic Sample", MB_OK | MB_ICONERROR );
  119.                             EndDialog( hDlg, 0 );
  120.                             return TRUE;
  121.                         }
  122.                     }
  123.                     break;
  124.  
  125.                 case IDC_EDIT_VARIABLE1:
  126.                 case IDC_EDIT_VARIABLE2:
  127.                     if ( HIWORD(wParam) == EN_KILLFOCUS )
  128.                     {
  129.                         if( FAILED( hr = SetVariable( hDlg, LOWORD(wParam) ) ) )
  130.                         {
  131.                             DXTRACE_ERR( TEXT("SetVariable"), hr );
  132.                             MessageBox( hDlg, "Error setting variable.  Sample will now exit.", 
  133.                                               "DirectMusic Sample", MB_OK | MB_ICONERROR );
  134.                             EndDialog( hDlg, 0 );
  135.                             return TRUE;
  136.                         }
  137.                     }
  138.                     break;
  139.  
  140.                 case IDOK:
  141.                     if ( HIWORD(wParam) == BN_CLICKED )
  142.                     {
  143.                         // This is sent by edit boxes to click the default button
  144.                         // (which we don't have) when Enter is pressed.
  145.                         HWND hwndFocus = GetFocus();
  146.                         int nIDDlgItem = 0;
  147.                         if ( hwndFocus == GetDlgItem(hDlg, IDC_EDIT_VARIABLE1) )
  148.                             nIDDlgItem = IDC_EDIT_VARIABLE1;
  149.                         else if ( hwndFocus == GetDlgItem(hDlg, IDC_EDIT_VARIABLE2) )
  150.                             nIDDlgItem = IDC_EDIT_VARIABLE2;
  151.  
  152.                         if ( nIDDlgItem != 0)
  153.                         {
  154.                             if( FAILED( hr = SetVariable( hDlg, nIDDlgItem ) ) )
  155.                             {
  156.                                 DXTRACE_ERR( TEXT("SetVariable"), hr );
  157.                                 MessageBox( hDlg, "Error setting variable.  Sample will now exit.", 
  158.                                                   "DirectMusic Sample", MB_OK | MB_ICONERROR );
  159.                                 EndDialog( hDlg, 0 );
  160.                                 return TRUE;
  161.                             }
  162.  
  163.                             // Select the value to show that Enter committed it
  164.                             SendMessage(hwndFocus, EM_SETSEL, 0, -1);
  165.                         }
  166.                     }
  167.                     break;
  168.  
  169.                 case IDCANCEL:
  170.                     EndDialog( hDlg, 0 );
  171.                     break;
  172.  
  173.                 default:
  174.                     return FALSE; // Didn't handle message
  175.             }
  176.             break;
  177.  
  178.         case WM_DESTROY:
  179.             // Cleanup everything
  180.             SAFE_DELETE( g_pMusicScript );
  181.             SAFE_DELETE( g_pMusicManager );
  182.             break; 
  183.  
  184.         default:
  185.             return FALSE; // Didn't handle message
  186.     }
  187.  
  188.     return TRUE; // Handled message
  189. }
  190.  
  191.  
  192.  
  193.  
  194. //-----------------------------------------------------------------------------
  195. // Name: OnInitDialog()
  196. // Desc: Initializes the dialogs (sets up UI controls, etc.)
  197. //-----------------------------------------------------------------------------
  198. HRESULT OnInitDialog( HWND hDlg )
  199. {
  200.     HRESULT hr;
  201.  
  202.     // Load the icon
  203.     HICON hIcon = LoadIcon( g_hInst, MAKEINTRESOURCE( IDR_MAINFRAME ) );
  204.  
  205.     // Set the icon for this dialog.
  206.     SendMessage( hDlg, WM_SETICON, ICON_BIG,   (LPARAM) hIcon );  // Set big icon
  207.     SendMessage( hDlg, WM_SETICON, ICON_SMALL, (LPARAM) hIcon );  // Set small icon
  208.  
  209.     g_pMusicManager = new CMusicManager();
  210.  
  211.     if( FAILED( hr = g_pMusicManager->Initialize( hDlg ) ) )
  212.         return hr;
  213.  
  214.     // Set the default media path (something like C:\MSSDK\SAMPLES\MULTIMEDIA\MEDIA)
  215.     // to be used as the search directory for finding DirectMusic content.
  216.     g_pMusicManager->SetSearchDirectory( DXUtil_GetDXSDKMediaPath() );
  217.  
  218.     // Fill the dropdown script choices
  219.     SendDlgItemMessage( hDlg, IDC_COMBO_SCRIPT, CB_ADDSTRING, 
  220.                         0, (LPARAM) "ScriptDemoBasic.spt" );
  221.     SendDlgItemMessage( hDlg, IDC_COMBO_SCRIPT, CB_ADDSTRING, 
  222.                         0, (LPARAM) "ScriptDemoBaseball.spt" );
  223.  
  224.     // Load ScriptDemoBasic
  225.     OnChangeScriptFile( hDlg, "ScriptDemoBasic.spt" );
  226.     SendDlgItemMessage( hDlg, IDC_COMBO_SCRIPT, CB_SELECTSTRING, 
  227.                         -1, (LPARAM) "ScriptDemoBasic.spt" );
  228.  
  229.     if( g_pMusicScript )
  230.     {
  231.         if( FAILED( hr = UpdateVariables( hDlg ) ) )
  232.             return hr;
  233.     }
  234.  
  235.     return S_OK;
  236. }
  237.  
  238.  
  239.  
  240.  
  241. //-----------------------------------------------------------------------------
  242. // Name: OnChangeScriptFile()
  243. // Desc: Called when the user changes script files from the dropdown.
  244. //-----------------------------------------------------------------------------
  245. HRESULT OnChangeScriptFile( HWND hDlg, TCHAR* strFileName )
  246. {
  247.     HRESULT hr;
  248.  
  249.     // Free any previous script, and make a new one
  250.     SAFE_DELETE( g_pMusicScript );
  251.  
  252.     // Have the loader collect any garbage now that the old 
  253.     // script has been released
  254.     g_pMusicManager->CollectGarbage();
  255.  
  256.     // Load the script file
  257.     if( FAILED( hr = g_pMusicManager->CreateScriptFromFile( &g_pMusicScript, strFileName ) ) )
  258.         return hr;
  259.  
  260.     // Get and display a string from the script describing itself.
  261.     // Here we will ignore any errors.  The script doesn't have to tell 
  262.     // about itself if it doesn't want to.
  263.     if( SUCCEEDED( g_pMusicScript->CallRoutine( TEXT("GetAbout") ) ) )
  264.     {
  265.         // Scripts are normally designed to return Number and Object variables.
  266.         // Retrieving a string is rarely used, and requires use of the more complicated
  267.         // VARIANT data type.
  268.         VARIANT varValue;
  269.         VariantInit(&varValue);
  270.         if( SUCCEEDED( g_pMusicScript->GetScript()->GetVariableVariant( L"About", 
  271.                                                                         &varValue, NULL ) ) )
  272.         {
  273.             if (varValue.vt == VT_BSTR)
  274.             {
  275.                 char strAbout[5000] = "";
  276.                 wcstombs( strAbout, varValue.bstrVal, 5000 );
  277.                 strAbout[4999] = '\0';
  278.                 SetDlgItemText( hDlg, IDC_EDIT_ABOUT, strAbout );
  279.             }
  280.  
  281.             // The returned VARIANT contains data fields that 
  282.             // must be freed or memory will be leaked
  283.             VariantClear(&varValue); 
  284.         }
  285.     }
  286.  
  287.     return S_OK;
  288. }
  289.  
  290.  
  291.  
  292.  
  293. //-----------------------------------------------------------------------------
  294. // Name: OnCallRoutine()
  295. // Desc: 
  296. //-----------------------------------------------------------------------------
  297. HRESULT OnCallRoutine( HWND hDlg, TCHAR* strRoutine )
  298. {
  299.     HRESULT hr;
  300.  
  301.     if( FAILED( hr = g_pMusicScript->CallRoutine( strRoutine ) ) )
  302.         return DXTRACE_ERR( TEXT("CallRoutine"), hr );
  303.  
  304.     if( FAILED( hr = UpdateVariables( hDlg ) ) )
  305.         return DXTRACE_ERR( TEXT("UpdateVariables"), hr );
  306.  
  307.     return S_OK;
  308. }
  309.  
  310.  
  311.  
  312.  
  313. //-----------------------------------------------------------------------------
  314. // Name: UpdateVariables()
  315. // Desc: 
  316. //-----------------------------------------------------------------------------
  317. HRESULT UpdateVariables( HWND hDlg )
  318. {
  319.     LONG lVal = 0;
  320.     HRESULT hr;
  321.  
  322.     if( FAILED( hr = g_pMusicScript->GetVariableNumber( TEXT("Variable1"), &lVal ) ) )
  323.         return DXTRACE_ERR( TEXT("GetVariableNumber"), hr );
  324.     SetDlgItemInt( hDlg, IDC_EDIT_VARIABLE1, lVal, TRUE );
  325.  
  326.     if( FAILED( hr = g_pMusicScript->GetVariableNumber( TEXT("Variable2"), &lVal ) ) )
  327.         return DXTRACE_ERR( TEXT("GetVariableNumber"), hr );
  328.     SetDlgItemInt( hDlg, IDC_EDIT_VARIABLE2, lVal, TRUE );
  329.  
  330.     return S_OK;
  331. }
  332.  
  333.  
  334.  
  335.  
  336. //-----------------------------------------------------------------------------
  337. // Name: SetVariable()
  338. // Desc: 
  339. //-----------------------------------------------------------------------------
  340. HRESULT SetVariable( HWND hDlg, int nIDDlgItem )
  341. {
  342.     HRESULT hr;
  343.     BOOL    fIntConvertOK = FALSE;
  344.     int     iVal = GetDlgItemInt( hDlg, nIDDlgItem, &fIntConvertOK, TRUE );
  345.     TCHAR* pstrVariable = (nIDDlgItem == IDC_EDIT_VARIABLE1) ? TEXT("Variable1") : TEXT("Variable2");
  346.  
  347.     hr = g_pMusicScript->SetVariableNumber( pstrVariable, iVal );
  348.     if( FAILED(hr) )
  349.         return hr;
  350.  
  351.     return S_OK;
  352. }
  353.